import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'

// 一级
import Layout from '@/views/layout.vue'
import Login from '@/views/login.vue'
import Register from '@/views/register.vue'
import Details from '@/views/details.vue'
import Collect from '@/views/collect.vue'
import Rent from '@/views/rent.vue'
import CityPage from '@/views/citypage.vue'

// 二级
import Homepage from '@/views/homepage.vue'
import FindPage from '@/views/findpage.vue'
import Information from '@/views/information.vue'
import Mine from '@/views/mine.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/Homepage',
      children: [
        {
          path: '/Homepage',
          component: Homepage
        },
        {
          path: '/FindPage',
          component: FindPage
        },
        {
          path: '/Information',
          component: Information
        },
        {
          path: '/Mine',
          component: Mine
        }
      ]

    },
    {
      path: '/Login',
      component: Login
    },
    {
      path: '/Register',
      component: Register
    },
    {
      path: '/Details',
      component: Details
    },
    {
      path: '/Collect',
      component: Collect
    },
    {
      path: '/Rent',
      component: Rent
    },
    {
      path: '/CityPage',
      component: CityPage
    }
  ]
})

const whiteList = ['/login', '/register', '/mine']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
